package com.assets.uhf.modelD2;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import com.assets.uhf.common.Activity0InventoryCommonAbstract;
import com.senter.iot.support.openapi.uhf.UhfD2;
import com.senter.iot.support.openapi.uhf.UhfD2.UII;
import com.senter.iot.support.openapi.uhf.UhfD2.UmdErrorCode;
import com.senter.iot.support.openapi.uhf.UhfD2.UmdFrequencyPoint;
import com.senter.iot.support.openapi.uhf.UhfD2.UmdOnIso18k6cRealTimeInventory;
import com.senter.iot.support.openapi.uhf.UhfD2.UmdRssi;
import com.senter.support.openapi.StKeyManager;

public class Activity0Inventory extends Activity0InventoryCommonAbstract {
    
 //   private final PartialWakeLocker mPartialWakeLocker = new PartialWakeLocker(this, TAG);
    /**
     * if the stop operation is caused by back button
     */
    private boolean finishOnStop = false;
    StKeyManager.ShortcutKeyMonitor monitorScan = StKeyManager.ShortcutKeyMonitor.isShortcutKeyAvailable(StKeyManager.ShortcutKey.Scan) ?
            StKeyManager.getInstanceOfShortcutKeyMonitor(StKeyManager.ShortcutKey.Scan) : null;
    private boolean isScaning;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       // getViews().setModes(InventoryMode.Custom);
        if (monitorScan != null) monitorScan.reset(this, listener, null);
    }
    
    @Override
    protected void uiOnInverntryButton() {
        if (worker.isInventroing()) {
            worker.stopInventory();
        } else {
          //  mPartialWakeLocker.wakeLockAcquire();
            getViews().setStateStarted();
            worker.startInventory();
        }
    }
    
    @Override//退出时调用
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK: {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (worker.isInventroing()) {
                        finishOnStop = true;
                        Log.i("222222222","2222222");
                        worker.stopInventory();
                    } else {
                        Log.i("222222222","ssssss");
                        UhfD2.getInstance().iso18k6cSetAccessEpcMatch(UhfD2.UmdEpcMatchSetting.newInstanceOfDisable());
                        finish();
                    }
                    return true;
                }
                break;
            }
            default:
                break;
        }
        
        return super.onKeyDown(keyCode, event);
    }
    
    private final ContinuousInventoryListener workerLisener = new ContinuousInventoryListener() {
        
        @Override
        public void onTagInventory(UII uii, UmdFrequencyPoint frequencyPoint, Integer antennaId, UmdRssi rssi) {

            addNewUiiMassageToListview(uii);
        }
        
        @Override
        public void onFinished() {
            if (finishOnStop) {
                UhfD2.getInstance().iso18k6cSetAccessEpcMatch(UhfD2.UmdEpcMatchSetting.newInstanceOfDisable());
                finish();
            } else {
               // mPartialWakeLocker.wakeLockRelease();
                getViews().setStateStoped();
            }
        }
    };
    private final ContinuousInventoryWorker worker = new ContinuousInventoryWorker(workerLisener);
    

    
    /**
     * dismiss waiting diaglog
     */

    
    private class ContinuousInventoryWorker {
        /**
         * go on inventoring after one inventory cycle finished.
         */
        private boolean goOnInventoring = true;
        
        private ContinuousInventoryListener mListener = null;
        
        private boolean isInventoring = false;
        
        /**
         * @param listener must no be null
         */
        public ContinuousInventoryWorker(ContinuousInventoryListener listener) {
            if (listener == null) {
                throw new NullPointerException();
            }
            mListener = listener;
        }
        
        public void startInventory() {
            goOnInventoring = true;
            isInventoring = true;
            
            UhfD2.getInstance().iso18k6cRealTimeInventory(1, new UmdOnIso18k6cRealTimeInventory() {
                
                @Override
                public void onFinishedWithError(UmdErrorCode error) {
                    onFinishedOnce();
                }
                
                @Override
                public void onFinishedSuccessfully(Integer antennaId, int readRate, int totalRead) {
                    onFinishedOnce();
                }
                
                private void onFinishedOnce() {
                    if (goOnInventoring) {
                        startInventory();
                    } else {
                        isInventoring = false;
                        mListener.onFinished();
                    }
                }
                
                @Override
                public void onTagInventory(UII uii, UmdFrequencyPoint frequencyPoint, Integer antennaId, UmdRssi rssi) {
                    mListener.onTagInventory(uii, frequencyPoint, antennaId, rssi);
                }
            });
            

        }
        
        public void stopInventory() {
            goOnInventoring = false;
        }
        
        public boolean isInventroing() {
            return isInventoring;
        }
    }
    
    private interface ContinuousInventoryListener {
        /**
         * will be called on finished completely
         */
        public void onFinished();
        
        public void onTagInventory(UII uii, UmdFrequencyPoint frequencyPoint, Integer antennaId, UmdRssi rssi);
    }
    
    

    //按键事件
    StKeyManager.ShortcutKeyMonitor.ShortcutKeyListener listener = new StKeyManager.ShortcutKeyMonitor.ShortcutKeyListener() {
        
        public void onKeyDown(int keycode, int repeatCount, StKeyManager.ShortcutKeyMonitor.ShortcutKeyEvent event) {
            if (isScaning == false) {
                uiOnInverntryButton();
                isScaning = true;
            }
        }
        
        public void onKeyUp(int keycode, int repeatCount, StKeyManager.ShortcutKeyMonitor.ShortcutKeyEvent event) {
            isScaning = false;
        }
        
    };

    @Override
    protected void onResume() {
        super.onResume();
        if (monitorScan != null) {
            monitorScan.startMonitor();
        }
        
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        if (monitorScan != null) {
            monitorScan.stopMonitor();
        }
        if (isFinishing()) {
          //  mPartialWakeLocker.wakeLockRelease();
        }
    }
    
    @Override
    protected void onDestroy() {
       // mPartialWakeLocker.wakeLockRelease();
        super.onDestroy();
    }

}
